<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()

// 用户信息
const user = ref<any>(null)

onMounted(() => {
  // 检查用户是否已登录
  const currentUser = localStorage.getItem('currentUser')
  if (!currentUser) {
    router.push('/login')
  } else {
    // 解析用户信息
    user.value = JSON.parse(currentUser)
  }
})

// 跳转到待办列表
const goToTodoList = () => {
  router.push('/todolist')
}

// 退出登录
const handleLogout = () => {
  localStorage.removeItem('currentUser')
  router.push('/login')
}
</script>

<template>
  <div class="profile-container">
    <div class="profile-card">
      <header class="profile-header">
        <h1>个人主页</h1>
        <button class="logout-btn" @click="handleLogout">退出登录</button>
      </header>
      
      <main class="profile-content" v-if="user">
        <div class="profile-info">
          <div class="avatar">
            <div class="avatar-circle">{{ user.username.charAt(0).toUpperCase() }}</div>
          </div>
          
          <div class="user-details">
            <h2>{{ user.username }}</h2>
            <p class="email">{{ user.email }}</p>
            <p class="login-time">
              登录时间: {{ new Date(user.loginTime).toLocaleString('zh-CN') }}
            </p>
          </div>
        </div>
        
        <div class="action-buttons">
          <button class="primary-btn" @click="goToTodoList">
            查看待办清单
          </button>
        </div>
        
        <div class="app-info">
          <h3>应用信息</h3>
          <p>这是您的个人主页，您可以从这里访问待办清单应用。</p>
          <div class="feature-list">
            <div class="feature-item">
              <span class="feature-icon">✓</span>
              <span>管理您的待办事项</span>
            </div>
            <div class="feature-item">
              <span class="feature-icon">✓</span>
              <span>跟踪任务完成状态</span>
            </div>
            <div class="feature-item">
              <span class="feature-icon">✓</span>
              <span>拖拽调整任务顺序</span>
            </div>
          </div>
        </div>
      </main>
      
      <div v-else class="loading">
        <p>加载用户信息中...</p>
      </div>
    </div>
  </div>
</template>

<style scoped>
.profile-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.profile-card {
  background: white;
  border-radius: 16px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 500px;
  overflow: hidden;
  animation: fadeIn 0.5s ease-out;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.profile-header {
  background: linear-gradient(135deg, #42b983 0%, #42b983 100%);
  padding: 25px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.profile-header h1 {
  color: white;
  font-size: 24px;
  margin: 0;
}

.logout-btn {
  padding: 8px 16px;
  background-color: rgba(255, 255, 255, 0.2);
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s ease;
}

.logout-btn:hover {
  background-color: rgba(255, 255, 255, 0.3);
}

.profile-content {
  padding: 30px;
}

.profile-info {
  display: flex;
  align-items: center;
  margin-bottom: 30px;
}

.avatar {
  margin-right: 20px;
}

.avatar-circle {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background: linear-gradient(135deg, #42b983 0%, #359469 100%);
  display: flex;
  justify-content: center;
  align-items: center;
  color: white;
  font-size: 32px;
  font-weight: 600;
}

.user-details h2 {
  margin: 0 0 10px 0;
  color: #333;
  font-size: 22px;
}

.email {
  color: #666;
  margin: 0 0 8px 0;
  font-size: 16px;
}

.login-time {
  color: #999;
  margin: 0;
  font-size: 14px;
}

.action-buttons {
  margin-bottom: 30px;
  text-align: center;
}

.primary-btn {
  background: linear-gradient(135deg, #42b983 0%, #359469 100%);
  color: white;
  border: none;
  border-radius: 10px;
  padding: 15px 30px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(66, 185, 131, 0.3);
}

.primary-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(66, 185, 131, 0.4);
}

.primary-btn:active {
  transform: translateY(0);
}

.app-info {
  border-top: 1px solid #eee;
  padding-top: 20px;
}

.app-info h3 {
  color: #333;
  margin: 0 0 15px 0;
  font-size: 18px;
}

.app-info p {
  color: #666;
  margin: 0 0 20px 0;
  line-height: 1.5;
}

.feature-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.feature-item {
  display: flex;
  align-items: center;
  color: #555;
}

.feature-icon {
  color: #42b983;
  font-weight: bold;
  margin-right: 10px;
  font-size: 18px;
}

.loading {
  padding: 40px;
  text-align: center;
  color: #666;
}

@media (max-width: 480px) {
  .profile-card {
    margin: 10px;
  }
  
  .profile-header,
  .profile-content {
    padding: 20px;
  }
  
  .profile-info {
    flex-direction: column;
    text-align: center;
  }
  
  .avatar {
    margin-right: 0;
    margin-bottom: 20px;
  }
}
</style>